/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-06-08 08:53:31
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2025-06-10 19:52:05
 * @FilePath: /udp/udp_dict_server/Dict.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include <unistd.h>
#include "Log.hpp"

using namespace log_ns;

const static std::string sep=": ";

class Dict
{

private:
    void LoadDict(const std::string& path)
    {
        std::ifstream in(path);
        if(!in.is_open())
        {
            LOG(FATAL,"open %s failed!\n",path.c_str());
            exit(-1);
        }

        std::string line;
        while(std::getline(in,line))
        {
            LOG(DEBUG,"load info: %s ,sucess\n",line.c_str());
            if(line.empty()) continue;
            auto pos=line.find(sep);
            if(pos==std::string::npos) continue;

            std::string key=line.substr(0,pos);
            if(key.empty()) continue;
            std::string value=line.substr(pos+sep.size());
            if(value.empty()) continue;

            _dict.insert(std::make_pair(key,value));

        }
        LOG(INFO, "load %s done\n", path.c_str());
        in.close();
    }

public:
    Dict(const std::string& dict_path):_dict_path(dict_path)
    {
        LoadDict(_dict_path);
    }
    ~Dict();

private:
    std::unordered_map<std::string,std::string> _dict;
    std::string _dict_path;
};